<script lang="ts" setup name="XtxCity">
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
import { ref, watch } from 'vue'
import { AreaList } from '@/types/goods'
import { IChangeResult } from '@/types/data'

// #3 接收 city
defineProps<{
  city: string
}>()

// 显示/隐藏
const active = ref(false)

const toggle = () => {
  active.value = !active.value
}

const target = ref(null)
// 如果点击的是 target 的外部就走回调
onClickOutside(target, () => {
  active.value = false
})

const areaList = ref<AreaList[]>([])
const cacheList = ref<AreaList[]>([])
const getCityList = async () => {
  const r = await axios.get<AreaList[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  areaList.value = r.data
  cacheList.value = r.data
}
getCityList()

// 记录选择的信息
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: '',
})

// #5 创建 emits，目的是为了修改外部的 city
const emits = defineEmits<{
  (e: 'changeCity', r: IChangeResult): void
}>()

const selectCity = (item: AreaList) => {
  if (item.level === 0) {
    changeResult.value.provinceCode = item.code
    changeResult.value.provinceName = item.name
  } else if (item.level === 1) {
    changeResult.value.cityCode = item.code
    changeResult.value.cityName = item.name
  } else if (item.level === 2) {
    changeResult.value.countyCode = item.code
    changeResult.value.countyName = item.name
    // 关闭弹框
    active.value = false
    // #6 把选择到的信息传递到父组件，父组件更新 city 变量
    emits('changeCity', changeResult.value)
  }
  // 把点击的当前项的 areaList 给 areaList
  areaList.value = item.areaList
}

watch(active, (newValue) => {
  if (newValue) {
    // 如果打开了，就把缓存的数据给 areaList
    areaList.value = cacheList.value
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <div
      class="select"
      :class="{
        active,
      }"
      @click="toggle"
    >
      <!-- #4 渲染 city -->
      <span class="value" v-if="city">{{ city }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span class="ellipsis" v-for="item in areaList" :key="item.code" @click="selectCity(item)">{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
